package com.bwie.service.impl;

import com.bwie.config.RedisConfig;
import com.bwie.mapper.UserMapper;
import com.bwie.pojo.Result;
import com.bwie.pojo.po.User;
import com.bwie.service.UserService;
import com.bwie.utils.AesUtil;
import com.bwie.utils.SendMsgUtil;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Random;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    public UserMapper userMapper;
    @Autowired
    public HttpServletRequest request;

    @Override
    public Result login(User user) throws Exception {
        if (StringUtil.isEmpty(user.getUserName())){
            return Result.error("参数为空");
        }
        if (StringUtil.isEmpty(user.getPassWord())){
            return Result.error("参数为空");
        }

        String encrypt = AesUtil.encrypt(user.getPassWord());
        User u= userMapper.login(user.getUserName());
        String decrypt = AesUtil.decrypt(encrypt);
        if (!decrypt.equals(u.getPassWord())&&!decrypt.equals(u.getUserName())){
            return Result.error("账号或密码不对");
        }
        request.getSession().setAttribute("user",u);
        return Result.success("登陆成功");
    }

    @Override
    public Result sendCode(String tel) {
        if (StringUtil.isEmpty(tel)){
            return Result.error("参数为空");
        }
        JedisPool jedisPool = RedisConfig.redisConn();
        Jedis jedis = jedisPool.getResource();

        String codeKey = "codeKey" + tel;
        String timeKey = "timeKey" + tel;
        User user= userMapper.selectTel(tel);
        if (!tel.equals(user.getTel())){
            return Result.error("手机号不存在");
        }
        String times = jedis.get(timeKey);
        if (times!=null){
            long time = new Date().getTime() - Long.valueOf(times);
            if (time<60*2*1000){
                return Result.error("2分钟稍后再试");
            }
        }
        Random random = new Random();
        int code = random.nextInt(9000) + 1000;
        String isOk = SendMsgUtil.sendSms(tel, String.valueOf(code));
        if (!isOk.equals("OK")){
            return Result.error("发送失败");
        }
        jedis.setex(codeKey,60*2,String.valueOf(code));
        jedis.set(timeKey,String.valueOf(new Date().getTime()));
        return Result.success("发送成功");
    }

    @Override
    public Result sendLogin(User user) {
        if (StringUtil.isEmpty(user.getTel())){
            return Result.error("参数为空");
        }
        if (StringUtil.isEmpty(user.getCode())){
            return Result.error("参数为空");
        }
        User u= userMapper.selectTel(user.getTel());
        if (!user.getTel().equals(u.getTel())){
            return Result.error("手机号不对");
        }
        JedisPool jedisPool = RedisConfig.redisConn();
        Jedis jedis = jedisPool.getResource();
        String code = jedis.get("codeKey" + user.getTel());

        if (!user.getCode().equals(code)){
            return Result.error("验证码错误");
        }
        request.getSession().setAttribute("user",u);
        return Result.success("登录成功");
    }
}
